package com.ruoyi.category.controller;

import com.ruoyi.category.domain.Category;
import com.ruoyi.category.service.ICategoryService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.api.DishApi;
import com.ruoyi.system.api.SetmealApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 菜品及套餐分类Controller
 * 
 * @author ruoyi
 * @date 2022-04-25
 */
@RestController
@RequestMapping("/category")
public class CategoryController extends BaseController
{
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private DishApi dishApi;
    @Autowired
    private SetmealApi setmealApi;

    /**
     * 查询菜品及套餐分类列表
     */
    @RequiresPermissions("category:category:list")
    @GetMapping("/list")
    public TableDataInfo list(Category category)
    {
        startPage();
        List<Category> list = categoryService.selectCategoryList(category);

        return getDataTable(list);
    }

    @GetMapping("/type")
    public List<Category> type()
    {
        startPage();
        List<Category> list = categoryService.find();
        return list;
    }

    /**
     * 导出菜品及套餐分类列表
     */
    @RequiresPermissions("category:category:export")
    @Log(title = "菜品及套餐分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Category category)
    {
        List<Category> list = categoryService.selectCategoryList(category);
        list.forEach(u-> System.out.println(u.getId()));
        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
        util.exportExcel(response, list, "菜品及套餐分类数据");
    }

    /**
     * 获取菜品及套餐分类详细信息
     */
    @RequiresPermissions("category:category:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(categoryService.selectCategoryById(id));
    }

    /**
     * 新增菜品及套餐分类
     */
    @RequiresPermissions("category:category:add")
    @Log(title = "菜品及套餐分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Category category)
    {
        return toAjax(categoryService.insertCategory(category));
    }

    /**
     * 修改菜品及套餐分类
     */
    @RequiresPermissions("category:category:edit")
    @Log(title = "菜品及套餐分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Category category)
    {
        return toAjax(categoryService.updateCategory(category));
    }

    /**
     * 删除菜品及套餐分类
     */
    @RequiresPermissions("category:category:remove")
    @Log(title = "菜品及套餐分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        boolean flag=true;
        String result="";
        for (Long id : ids) {
            AjaxResult dishApiInfo = dishApi.getInfo(id);
            AjaxResult setmealApiInfo = setmealApi.getInfo(id);
            ArrayList dishmap= (ArrayList) dishApiInfo.get("data");
            System.out.println(dishmap);
            System.out.println(dishmap);
            ArrayList setmealmap= (ArrayList) setmealApiInfo.get("data");
            for (int i = 0; i < dishmap.size(); i++) {
                if(null!=dishmap&&!dishmap.contains("id")){
                    result="该菜品分类下还有菜品不能删除";
                    flag=false;
                }
            }
            for (int i = 0; i < setmealmap.size(); i++) {
                if(null!=setmealmap&&!setmealmap.contains("id")){
                    result="该套餐分类下还有菜品不能删除";
                    flag=false;
                }
            }
        }
        if(flag){
            return toAjax(categoryService.deleteCategoryByIds(ids));
        }
        return AjaxResult.error(result);
    }
}
